<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品规格类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 * @version 1.0
 */

class goodsSpec {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 保存和更新规格组
     *
     * @return unknown
     */
    public function saveSpecGroupInfo ( $arg_array ) {
        //检查参数
        if (!isset($arg_array['specGroupName'] ,$arg_array['sortOrder'] ,$arg_array['creator'], $arg_array['specGroupID'], $arg_array['isColor']))
            return array( 'code' => 400, 'message' => '参数错误');

        $sortOrder 	 = $arg_array['sortOrder'] ;			//排序序号
        $specGroupID = $arg_array['specGroupID'] ;			//规格组号
        $isColor = $arg_array['isColor'];                   //是否为颜色规格属性
        $specGroupName = $arg_array['specGroupName'];       //规格组名字
        $useFlg = $arg_array['useFlg'];                     //启用状态
        $result = $this->dbca->stmt_query("SELECT * FROM goods_spec_group WHERE specGroupName = ?","s",$specGroupName);
        try{
            $specGroupInfo = new entity('goods_spec_group') ;
            //如果规格组号不为空则查询检查规格组号
            if($specGroupID != 0){
                //if( $specGroupID == 1 || $specGroupID == 2 )
                //throw new Exception('规格组「颜色,尺码」,不允许操作' , 400);

                $specGroupInfo->specGroupID = $specGroupID ;
                if(!$this->dbca->fetch($specGroupInfo)){
                    throw new Exception(sprintf('规格组号「%d」,的规格组不存在,请审核后再操作',$str_array[0]) , 400);
                }
                //检查规格组排序有没有被使用
                $issetSO = $this->dbca->stmt_query('SELECT COUNT(*) count FROM goods_spec_group WHERE sortOrder=? AND specGroupID!=?','ii' ,$sortOrder, $specGroupID) ;
                if($issetSO[0]->count > 0)
                    throw new Exception('排序序号已经被使用' , 400);

                $specGroupInfo->editor = $arg_array['creator'] ;
            }else {
                //检查规格组排序有没有被使用
                $issetSO = $this->dbca->stmt_query('SELECT COUNT(*) count FROM goods_spec_group WHERE sortOrder=?','i' ,$sortOrder) ;
                if($issetSO[0]->count > 0){
                    throw new Exception('排序序号已经被使用' , 400);
                }
                if($result){
                    throw new Exception('规格组名称已经存在,请审核后再操作',400);
                }
                $specGroupInfo->creator = $arg_array['creator'] ;
            }

            //保存和更新规格组信息
            $specGroupInfo->specGroupName = trim($arg_array['specGroupName']) ;
            $specGroupInfo->sortOrder 	  = $arg_array['sortOrder'] ;
            $specGroupInfo->isColor       = $arg_array['isColor'];
            $specGroupInfo->useFlg        = $arg_array['useFlg'];
            $this->dbca->save( $specGroupInfo , 'goods' ) ;
            return array( 'code' => 200, 'message' => '规格组保存成功' ) ;
        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }

    /**
	 * 删除规格组
	 *
	 */
	public function deleteSpecGroup ( $arg_array ) {
		
		//规格组号串检查
		if (!isset($arg_array['delete_str'])) {
            return array( 'code' => 400, 'message' => '缺少参数' );
		}    
            
		//将规格组号串截成数组
        $delete_array = explode('_', $arg_array['delete_str']);
        
        if (empty($delete_array)) {
            return array( 'code' => 400, 'message' => '请选择需要删除的商品规格' );
        }
        
        //检查当前规格是否存在商品
         foreach( $delete_array as &$c_l ) {
			$spec_group = new entity('goods_spec_group');
			$spec_group->specGroupID = $c_l;
			if (!$this->dbca->fetch($spec_group)) {
				return array('code' => 400, 'message' => sprintf('商品规格号「%d」信息未找到，请核实', $c_l));
			}
			
			//是否存在商品
			$group_relation = $this->dbca->stmt_query('SELECT * FROM goods_spec_relation WHERE specGroupID = ?','i',$c_l);	
			if ($group_relation) {
				return array('code' => 400, 'message' => sprintf('商品规格「%s」存在商品，不能删除', $spec_group->specGroupName));
			}
         }
        
        try {
        	$this->dbca->beginTransaction();
        	
			//循环删除规格组
            foreach( $delete_array as &$g_l ) {

                //查询规格组信息
                $sql_del_specgroup_info = 'DELETE FROM goods_spec_group WHERE specGroupID=?';
                
                $specgroup_del_result = $this->dbca->stmt_query($sql_del_specgroup_info,'i',$g_l);
                
				$sql_del_spec_info = 'DELETE FROM goods_spec WHERE specGroupID=?';
				
                $spec_del_result = $this->dbca->stmt_query($sql_del_spec_info,'i',$g_l);
            }
            
            $this->dbca->commit();
            
            return array( 'code' => 200, 'message' => '商品规格删除成功' );
        } catch (Exception $e) {
        	$this->dbca->rollBack();
        	error_log($e->getTraceAsString());
            if ($e->getCode() > 0) 
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
	}

    /**
	 * 商品规格列表 
	 *
	 * @param unknown_type $arg_array 规格组参数
	 * @return unknown
	 */
	public function goodsSpecList ( &$arg_array ) {
		//sql字符串
        $sql = '';
        
        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //规格名称
        if (isset($arg_array['specName']) && trim($arg_array['specName']) != '') {
            $sql .= ' AND sc.specName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['specName']) . '%';
        }

        //规格ID
        if (isset($arg_array['specID']) && is_numeric($arg_array['specID'])) {
            $sql .= ' AND sc.specID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['specID'];
        }

        //规格组号
        if (isset($arg_array['specGroupID']) && trim($arg_array['specGroupID']) != '') {
            $sql .= ' AND sc.specGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['specGroupID'];
        }
        
        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计促销行数
                $search_sql  = 'SELECT COUNT(*) AS count FROM goods_spec sc ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count  = $count_array[0]->count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY sc.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY sc.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询商品促销信息

            $search_sql = "SELECT sc.specID,sc.specName,sc.specVal,sc.specGroupID,sc.sortOrder,sc.useFlg,scg.specGroupName FROM goods_spec sc
						   INNER JOIN goods_spec_group scg ON sc.specGroupID=scg.specGroupID" . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            
            if ($temp_item_array) {
				foreach($temp_item_array as &$item) {
				    $item_array[] = array(
				        'specID' 	   => $item->specID,
				        'specName' 	   => $item->specName,
				        'specGroupID'  => $item->specGroupID,
				        'sortOrder'    => $item->sortOrder,
				        'useFlg' 	   => $item->useFlg,
				        'specVal' 	   => $item->specVal
				    );
				}
            }
			
			return isset($item_array) ? $item_array : null;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	}

    /**
     * 删除规格
     *
     */
    public function deleteSpec ( $arg_array ) {
        //规格号串检查
        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的规格' );

        //将规格号串截成数组
        $delete_array = explode('_', $arg_array['delete_str']);

        try {
            //循环删除规格
            foreach( $delete_array as &$s_l ) {
                if (!is_numeric( $s_l ))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询规格信息
                $specInfo = new entity('goods_spec');
                $specInfo->SpecID = $s_l;
                if ($this->dbca->fetch( $specInfo ))
                    $this->dbca->delete( $specInfo );
            }
            return array( 'code' => 200, 'message' => '删除成功' );
        } catch (Exception $e) {
            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );
        }
    }

    /**
     * 规格的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
    public function statusGoodsSpec ( $arg_array ) {
        //检查参数
        if (!isset($arg_array['useFlg_str']) || $arg_array['useFlg_str'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        $str_array = explode('_', $arg_array['useFlg_str']);

        //查询规格是否存在或已经被操作
        $specInfo = new entity('goods_spec');
        $specInfo->specID = $str_array[0] ;
        if (!$this->dbca->fetch( $specInfo ))
            return array( 'code' => 400, 'message' => sprintf('规格号「%d」,的规格不存在,请审核后再操作',$str_array[0]));

        //检查规格状态是否已经修改
        if($specInfo->useFlg == $str_array[1] && $str_array[1] == 1)
            return array( 'code' => 400, 'message' => sprintf('规格号「%d」,的规格已启用',$str_array[0]));

        if($specInfo->useFlg == $str_array[1] && $str_array[1] == 0)
            return array( 'code' => 400, 'message' => sprintf('规格号「%d」,的规格已禁用',$str_array[0]));

        //修改规格状态
        $specInfo->useFlg = $str_array[1] ;
        $this->dbca->save( $specInfo , 'goods' );

        if($str_array[1] == 1)
            return array( 'code' => 200, 'message' => sprintf('规格号「%d」,的规格启用成功',$str_array[0]) );

        else if ($str_array[1] == 0)
            return array( 'code' => 200, 'message' => sprintf('规格号「%d」,的规格禁用成功',$str_array[0]) );
    }

    /**
     * 保存和更新规格
     *
     * @return unknown
     */
    public function saveGoodsSpec ( $arg_array ) {
        global $G_PGLOB;
		//检查参数
		if (!isset($arg_array['specID'], $arg_array['specName'], $arg_array['sortOrder'], $arg_array['isColor'], $arg_array['spec_array'],$arg_array['specVal'])) {
			return array('code' => 400, 'message' => '缺少参数');
		}
		
		//检查规格名称是否重复
		$check_spec_group = new entity('goods_spec_group');
		$check_spec_group->specGroupName = $arg_array['specName'];
		if ($this->dbca->fetch($check_spec_group)) {
			if (!empty($arg_array['specID'])) {
				if ($check_spec_group->specGroupID != $arg_array['specID']) {
					return array('code' => 400, 'message' => sprintf('商品规格「%s」已经存在', $arg_array['specName']));
				}
			} else {
				return array('code' => 400, 'message' => sprintf('商品规格「%s」已经存在', $arg_array['specName']));
			}
		}
		
		//检查排序序号是否重复
		$sort_spec_group = new entity('goods_spec_group');
		$sort_spec_group->sortOrder = $arg_array['sortOrder'];
		if ($this->dbca->fetch($sort_spec_group)) {
			if (!empty($arg_array['specID'])) {
				if ($sort_spec_group->specGroupID != $arg_array['specID']) {
					return array('code' => 400, 'message' => sprintf('商品排序序号「%d」已经存在', $arg_array['sortOrder']));
				}
			} else {
				return array('code' => 400, 'message' => sprintf('商品排序序号「%d」已经存在', $arg_array['sortOrder']));
			}
		}
		
		$goods_spec_group = new entity('goods_spec_group');
		if (!empty($arg_array['specID'])) {
			$goods_spec_group->specGroupID = $arg_array['specID'];
			if (!$this->dbca->fetch($goods_spec_group)) {
				return array('code' => 400, 'message' => '商品规格信息不存在');
			}	
			
			$goods_spec_group->editor = $arg_array['creator'];

            $allspecName = $this->dbca->stmt_query('SELECT specName FROM goods_spec WHERE specGroupID = ?','i',$arg_array['specID']);

		} else {
			$goods_spec_group->creator = $arg_array['creator'];
		}
		$goods_spec_group->specGroupName = $arg_array['specName'];
		$goods_spec_group->sortOrder     = $arg_array['sortOrder'];
		$goods_spec_group->useFlg        = 1;
		$goods_spec_group->isColor       = $arg_array['isColor'];
		
		$this->dbca->beginTransaction();
		try{
			$this->dbca->save($goods_spec_group);
			
			//获取规格ID
			if (!empty($arg_array['specID'])) {
				$specGroupID = $arg_array['specID'];
			} else {
				$specGroup = $this->dbca->stmt_query('SELECT MAX(specGroupID) max_id FROM goods_spec_group');
				$specGroupID = $specGroup[0]->max_id;
			}

            if (isset($allspecName) && !empty($allspecName)) {

                foreach ($allspecName as $v) {
                    $specName = $v->specName;
                    if (in_array($specName,$arg_array['spec_array'])) {
                    	$goods_spec = new entity('goods_spec');
                    	$goods_spec->specGroupID = $arg_array['specID'];
                    	$goods_spec->specName = $specName;
                    	if (!$this->dbca->fetch($goods_spec)) {
                    		throw new Exception('查不到规格信息',404);
                    	}
                    	$k = array_search($specName, $arg_array['spec_array']);
                    	$goods_spec->editor  = $G_PGLOB->currentUserID;
                    	$goods_spec->specVal = $arg_array['specVal'][$k];
                    	
                    	$this->dbca->save($goods_spec);
                    	
//                    	if (!$this->dbca->save($goods_spec)) {
//                    		throw new Exception('保存规格信息失败',400);
//                    	}
                    	
                    	unset($arg_array['spec_array'][$k]);
                    	unset($arg_array['specVal'][$k]);

                    } else {
                        $this->dbca->stmt_query('DELETE FROM goods_spec WHERE specGroupID = ? AND specName = ?','is',array($arg_array['specID'],$specName));
                    }
                }
            }
            
            //获取规格值最大排序
            $max_sort = $this->dbca->stmt_query('SELECT MAX(sortOrder)+1 max_sort FROM goods_spec');
            if (empty($max_sort[0]->max_sort)) {
            	$max_sort = 1;
            } else {
            	$max_sort = $max_sort[0]->max_sort;
            }
			
			if (!empty($arg_array['spec_array']) && !empty($arg_array['specVal'])) {
				$arg_array['spec_array'] = array_unique($arg_array['spec_array']);
				
				foreach ($arg_array['spec_array'] as $k=>$s_l) {
                    foreach ($arg_array['specVal'] as $k1=>$v1) {
                        if ($k == $k1) {
                            
                            $goods_spec = new entity('goods_spec');
                            $goods_spec->creator     = $arg_array['creator'];
                            $goods_spec->specName    = $s_l;
                            $goods_spec->specGroupID = $specGroupID;
                            $goods_spec->sortOrder   = $max_sort;
                            $goods_spec->specVal     = $v1;
                            $goods_spec->useFlg      = 1;

                            $this->dbca->save($goods_spec);
                            
                            $max_sort++;
                        }
                    }

				}
			}
			
			$this->dbca->commit();
			if (!empty($arg_array['specID'])) {
				return array('code' => 200, 'message' => '商品规格更新成功', 'specID' => $arg_array['specID'], 'sortOrder' => $arg_array['sortOrder']+1);
			} else {
				return array('code' => 200, 'message' => '商品规格保存成功', 'specID' => '', 'sortOrder' => $arg_array['sortOrder']+1);
			}
		
		} catch(Exception $e) {
			
			$this->dbca->rollBack();
			
			return array('code' => 400, 'message' => $e->getMessage());
		}
	}
	
	/**
	 * 规格最大序号
	 *
	 */
	public function maxSortOrder(){
		$sortOrder = &$this->dbca->stmt_query('SELECT MAX(sortOrder) sort FROM goods_spec_group');
		if (is_null($sortOrder[0]->sort)) {
			return 1;
		} else {
			return $sortOrder[0]->sort + 1;
		}
	}
	
	/**
	 * 商品规格组列表 
	 *
	 * @param unknown_type $arg_array 规格组参数
	 * @return unknown
	 */
	public function goodsSpecGroupList ( &$arg_array ) {
	
		 //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //规格组号
        if (isset($arg_array['specGroupID']) && is_numeric($arg_array['specGroupID'])) {
            $sql .= ' AND specGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['specGroupID'];
        }

        //规格组名称
        if (isset($arg_array['specGroupName']) && !empty($arg_array['specGroupName'])) {
            $sql .= ' AND specGroupName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['specGroupName']) . '%';
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_spec_group ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_spec_group ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
            	
				$specVal = '';
            	
        		$specArray = array(
        			'specGroupID' => $item->specGroupID,
        			'sortorder' => 'asc',
        			'sortname' => 'sortOrder'
        		);
        		$specData = $this->goodsSpecList($specArray);
        		if ($specData) {
        			if (isset($arg_array['isSpec']) && $arg_array['isSpec'] == 1) {
	        			foreach ($specData as $key=>$spec){
	        				if (count($specData) == $key+1) {
	        					$specVal .= $spec['specName'];
	        				} else {
	        					$specVal .= $spec['specName'] . '，';
	        				}
	        			}
        			}
            		
            	}
            	
                $item_array[] = array(
                    'specGroupID' 	=> $item->specGroupID,
                    'specGroupName' => $item->specGroupName,
                    'sortOrder' 	=> $item->sortOrder,
                    'useFlg' 		=> $item->useFlg,
                    'isColor' 		=> $item->isColor,
                    'specVal'	    => $specVal,
                    'specArray'		=> $specData
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	}
	
	/**
     * 规格组的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */

    public function changeUse($arg_array){

        if (!isset($arg_array['specGroupID']) || $arg_array['specGroupID'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        //查询规格组是否存在或已经被操作
        $tagGroupInfo = new entity('goods_spec_group');
        $tagGroupInfo->specGroupID = $arg_array['specGroupID'] ;
        if (!$this->dbca->fetch( $tagGroupInfo ))
            return array( 'code' => 400, 'message' => sprintf('商品规格号「%d」信息未找到，请核实',$arg_array['specGroupID'] ));

        //检查规格组状态是否已经修改
        if($tagGroupInfo->useFlg == $arg_array['useFlg']  && $arg_array['useFlg'] == 1)
            return array( 'code' => 400, 'message' => sprintf('商品规格「%s」已启用',$tagGroupInfo->specGroupName ));

        if($tagGroupInfo->useFlg == $arg_array['useFlg'] && $arg_array['useFlg'] == 0)
            return array( 'code' => 400, 'message' => sprintf('商品规格「%s」已禁用',$tagGroupInfo->specGroupName ));

        //修改规格组状态
        $tagGroupInfo->useFlg = $arg_array['useFlg'] ;
        $this->dbca->save( $tagGroupInfo , 'goods_spec_group' );

        if($arg_array['useFlg'] == 1)
            return array( 'code' => 200, 'message' => sprintf('商品规格「%s」启用成功',$tagGroupInfo->specGroupName ) );

        else if ($arg_array['useFlg'] == 0)
            return array( 'code' => 200, 'message' => sprintf('商品规格「%s」禁用成功',$tagGroupInfo->specGroupName ) );
    }
    
    
    /**
     * 查询商品规格
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchGoodsSpec( &$arg_array ) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//规格组ID
    	if (isset($arg_array['specGroupID']) && is_numeric($arg_array['specGroupID'])) {
    		$sql .= ' AND specGroupID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['specGroupID'];
    	}
    
    	//规格ID
    	if (isset($arg_array['specID']) && is_numeric($arg_array['specID'])) {
    		$sql .= ' AND specID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['specID'];
    	}
    
    	//使用标志
    	if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
    		$sql .= ' AND useFlg = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['useFlg'];
    	}
    
    	//扩展
    	if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
    		$sql .= $arg_array['extend'];
    	}
    
    	if ($sql) {
    		$sql = 'WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计数量
    			$search_sql = 'SELECT COUNT(*) AS item_count FROM goods_spec ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count = $count_array[0]->item_count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询信息
    		$search_sql = 'SELECT * FROM goods_spec ' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    		if (!$temp_item_array)
    			return null;
    
    		foreach($temp_item_array as &$item) {
    
    			$item_array[] = array(
    					'specGroupID' => $item->specGroupID,
    					'specName' => $item->specName,
    					'specID' => $item->specID,
    					'sortOrder' => $item->sortOrder,
    					'useFlg' => $item->useFlg
    			);
    
    		}
    
    		return $item_array;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    
    }
    
    /**
     * 查询商品规格映射
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchGoodsSpecRelation( &$arg_array ) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//规格组ID
    	if (isset($arg_array['specGroupID']) && is_numeric($arg_array['specGroupID'])) {
    		$sql .= ' AND specGroupID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['specGroupID'];
    	}
    
    	//规格ID
    	if (isset($arg_array['specID']) && is_numeric($arg_array['specID'])) {
    		$sql .= ' AND specID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['specID'];
    	}
    
    	//三级网购号
    	if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
    		$sql .= ' AND postID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['postID'];
    	}
    	
    	//二级网购号
    	if (isset($arg_array['twoPostID']) && is_numeric($arg_array['twoPostID'])) {
    		$sql .= ' AND twoPostID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['twoPostID'];
    	}
    
    	//扩展
    	if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
    		$sql .= $arg_array['extend'];
    	}
    
    	if ($sql) {
    		$sql = 'WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计数量
    			$search_sql = 'SELECT COUNT(*) AS item_count FROM goods_spec_relation ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count = $count_array[0]->item_count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询信息
    		$search_sql = 'SELECT * FROM goods_spec_relation ' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    		if (!$temp_item_array)
    			return null;
    
    		foreach($temp_item_array as &$item) {
    
    			$item_array[] = array(
    					'postID' => $item->postID,
    					'specGroupID' => $item->specGroupID,
    					'specGroupName' => $item->specGroupName,
    					'specID' => $item->specID,
    					'specName' => $item->specName,
    					'twoPostID' => $item->twoPostID
    			);
    
    		}
    
    		return $item_array;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    
    }
}